<?php

class Services_model extends CI_Model {
    
    
    
    function user_reg($data) 
    {
        try 
        {
            $this->db->insert(USERS_TABLE, $data);
            return $this->db->insert_id();
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function user_reg() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function login($data) 
    {
        try {
            $result = $this->db->get_where(USERS_TABLE, $data);
            if ($result->num_rows() > 0)
                return $result->row()->id;
            else
                return FALSE;
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function login() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function user_get($data)
    {
        try {
            $result = $this->db->get_where(USERS_TABLE,$data);
            if ($result->num_rows() > 0)
                return $result->result_array();
            else
                return FALSE;
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function user_get() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function user_edit($data)
    {
        try 
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $this->db->update(USERS_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function user_edit() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    

    

    
    function user_disable($data)
    {
        try 
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $data['state'] = USER_INACTIVE;
            $this->db->update(USERS_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function user_delete() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function users_get()
    {
        try {
            $users = array();
            $result = $this->db->get(USERS_TABLE);
            if ($result->num_rows() > 0)
            {
                $count = 0;
                foreach ($result->result_array() as $row)
                {
                    $users[$count] = $row;
                    $count += 1;
                }
            }
            return $users;
            
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function users_get() with parameters - ', '');
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function get_products_from_store($data)
    {
        try {
            $products = array();
            $result = $this->db->get_where(PRODUCTS_TABLE, $data);
            if ($result->num_rows() > 0)
            {
                $count = 0;
                foreach ($result->result_array() as $row)
                {
                    $products[$count] = $row;
                    $count += 1;
                }
            }
            return $products;
            
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function get_products_from_store() with parameters - ', '');
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function get_all_stores()
    {
        try {
            $stores = array();
            $this->db->where('state',STORE_ACTIVE);
            $result = $this->db->get(STORES_TABLE);
            if ($result->num_rows() > 0)
            {
                $count = 0;
                foreach ($result->result_array() as $row)
                {
                    $stores[$count] = $row;
                    $count += 1;
                }
            }
            return $stores;
            
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function get_all_stores() with parameters - ', '');
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function store_reg($data) 
    {
        try 
        {
            $this->db->insert(STORES_TABLE, $data);
            //return $this->db->insert_id();
            return true;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function store_reg() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function store_edit($data)
    {
        try 
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $this->db->update(STORES_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function store_edit() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    function store_del($data)
    {
        try 
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $data['state'] = STORE_INACTIVE;
            $this->db->update(STORES_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function store_del() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function get_my_stores($data)
    {
        try {
            $stores = array();
            $this->db->where('state',STORE_ACTIVE);
            $result = $this->db->get_where(STORES_TABLE, $data);
            if ($result->num_rows() > 0)
            {
                $count = 0;
                foreach ($result->result_array() as $row)
                {
                    $stores[$count] = $row;
                    $count += 1;
                }
            }
            return $stores;
            
        } catch (Exception $e) {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function get_my_stores() with parameters - ', '');
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function product_add($data) 
    {
        try 
        {
            $this->db->insert(PRODUCTS_TABLE, $data);
            //return $this->db->insert_id();
            return true;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function product_add() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function product_edit($data)
    {
        try 
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $this->db->update(PRODUCTS_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function product_edit() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }
    
    
    function product_del($data)
    {
        try 
        {
            $this->db->delete(PRODUCTS_TABLE, $data);
            return TRUE;
        } 
        catch (Exception $e) 
        {
            $message = sprintf(ERROR_FORMAT, ' Executing Services_model function product_del() with parameters - ', join(' | ', $data));
            log_message('error', $message, 'Exception:' . $e->getMessage());
            return FALSE;
        }
    }

}

/* End of file services_model.php */
/* Location: ./application/models/services_model.php */
